<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<div id="updateDialog" class="container-fluid">
    <div class="row">
        <form id="updateForm" method="post" class="form-horizontal" action="">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="name">
                    <span class="help-inline"><font color="red">*</font> </span>
                    公号名称：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="name" name="name" value="${wechatMpWebJwid.name}" placeholder="名称" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="accountType">
                    类型：</label>
                <div class="col-sm-5">
                    <select id="accountType" name="accountType" class="form-control">
                        <option value="1" <c:if test="${wechatMpWebJwid.accountType==1}">selected="selected"</c:if>>微信服务号</option>
                        <option value="2" <c:if test="${wechatMpWebJwid.accountType==2}">selected="selected"</c:if>>>微信订阅号</option>
                        <option value="3" <c:if test="${wechatMpWebJwid.accountType==3}">selected="selected"</c:if>>>微信小程序</option>
                        <option value="4" <c:if test="${wechatMpWebJwid.accountType==4}">selected="selected"</c:if>>>支付宝小程序</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="agent" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    默认代理：</label>
                <div class="col-sm-5">
                    <div id="agent" data-id="${agent.id}" data-name="${agent.name}" ></div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="mchid" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    商户Id：</label>
                <div class="col-sm-5">
                    <select id="mchid" name="mchid" class="form-control">
                        <c:forEach var="payMchInfo" items="${payMchInfos}">
                            <option value="${payMchInfo.mchid}" <c:if test="${wechatMpWebJwid.mchid==payMchInfo.mchid}">selected</c:if>>${payMchInfo.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="jwid">
                    <span class="help-inline"><font color="red">*</font> </span>
                    原始ID：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="jwid" name="jwid" value="${wechatMpWebJwid.jwid}" placeholder="原始ID" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="weixinAppid">
                    <span class="help-inline"><font color="red">*</font> </span>
                    appid：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="weixinAppid" name="weixinAppid" value="${wechatMpWebJwid.weixinAppid}" placeholder="appid" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="weixinAppsecret">
                    <span class="help-inline"><font color="red">*</font> </span>
                    AppSecret：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="weixinAppsecret" name="weixinAppsecret" value="${wechatMpWebJwid.weixinAppsecret}" placeholder="AppSecret" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="token">
                    TOEKN(令牌)：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="token" name="token" value="${wechatMpWebJwid.token}" placeholder="TOEKN(令牌)" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="authStatus">
                    是否认证：</label>
                <div class="col-sm-5">
                    <select id="authStatus" name="authStatus" class="form-control">
                        <option value="1" <c:if test="${wechatMpWebJwid.authStatus==1}">selected="selected"</c:if>>是</option>
                        <option value="0" <c:if test="${wechatMpWebJwid.authStatus==0}">selected="selected"</c:if>>>否</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="mainpage">
                    跳转页：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="mainpage" name="mainpage" value="${wechatMpWebJwid.mainpage}" placeholder="跳转页" />
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="updateDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function validateForm(){
        return $( "#updateForm" ).validate( {
            rules: {
                name: {
                    required: true,
                    maxlength: 64
                },
                jwid: {
                    required: true,
                    maxlength: 64
                },
                appid: {
                    required: true,
                    maxlength: 64
                },
                weixinAppSecret: {
                    required: true,
                    maxlength: 64
                },
                agentId: "required",
                mchid: "required"
            },
            messages: {
                name: {
                    required: "名称是必填项。",
                    maxlength: "名称长度超出64个字符。"
                },
                jwid: {
                    required: "原始ID是必填项。",
                    maxlength: "原始ID长度超出64个字符。"
                },
                appid: {
                    required: "appid是必填项。",
                    maxlength: "appid长度超出50个字符。"
                },
                weixinAppSecret: {
                    required: "AppSecret是必填项。",
                    maxlength: "AppSecret长度超出255个字符。"
                },
                agentId: "默认代理是必填项。",
                mchid: "商户ID是必填项。"
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

    $(function(){
        //初始化树形select控件
        <%--$("#agent").bootstrapCombotree({--%>
            <%--data: ${upmsAgents},//data应符合实例的data格式--%>
            <%--defaultLable:"请选择",--%>
            <%--name : 'agentId'//combotree值域的name，可以用在表单提交--%>
        <%--})--%>
        $("#agent").ztreeSelect({
            name:"agentId",
            url: '${basePath}/manage/agentTree',
            params: {isLeaf: 1},
            renderId: "agent",
            callback: function (nodeIds, treeNode) {
            }
        })
    });

    function createSubmit() {
        if(validateForm().form()){
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/thirdapp/update/${wechatMpWebJwid.id}',
                data: $('#updateForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        updateDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>